import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import svgLoader from "vite-svg-loader";
import pxtovm from "postcss-px-to-viewport";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import prefetchPlugin from "vite-plugin-bundle-prefetch";
const pxtovmPlugin = pxtovm({
  viewportWidth: 2300,
  viewportUnit: "vw",
});



export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_PROXY_TARGET, VITE_APP_BASE_API } = env;
  return {
    plugins: [
      vue(),
      prefetchPlugin(),
      svgLoader(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    base: "./",
    server: {
      port: 3000,
      open: true,
      host: true,
      cors: true,
      proxy: {
        [VITE_APP_BASE_API]: {
          target: VITE_PROXY_TARGET,
          changeOrigin: true,
          rewrite: (path) => path.replace(RegExp(`^${VITE_APP_BASE_API}`), ""),
        },
      },
    },

    css: {
      postcss: {
        plugins: [pxtovmPlugin],
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    build: {
      sourcemap: false,
      // 消除打包大小超过500kb警告
      chunkSizeWarningLimit: 4000,
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, "./index.html"),
        },
        // 静态资源分类打包
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        },
      },
    },
  };
});
